package cn.kgc.itrip.auth.controller;

import cn.kgc.itrip.auth.service.IUserService;
import cn.kgc.itrip.auth.service.TokenService;
import cn.kgc.itrip.beans.common.ServerResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 阿泽
 */
@Controller
@RequestMapping("/api")
public class LoginController {
    @Resource
    IUserService iUserService;
    @Resource
    TokenService tokenService;

    @RequestMapping(value = "/dologin", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse dologin(String name, String password, HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent");
        ServerResponse userserverResponse = iUserService.usercode(name, password, userAgent);
        return userserverResponse;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse logout(HttpServletRequest httpServletRequest) {
        return tokenService.delToken(httpServletRequest.getHeader("token"));
    }
}
